define(function(require,exports,module){
    'use strict';

    var React = require('react');
    var $ = require('jquery');
    var bootstrap = require('bootstrap');
    var language = require('datetimepickerLanguage');
    var datetimepicker = require('datetimepicker');
    var publicObj = require('lib/public');
    var token = publicObj.get_token();
    /*调用AreaBasicDiagram组件*/
    var ApiAreaDiagram = require('components/diagram/AreaBasicDiagram');
	var ReactLoading = require('../../partial/ReactLoading');

    module.exports = React.createClass({
        loadData:function(start_date,end_date){
            if(start_date && end_date){
                this.setState({'data':[]});
                this.setState({'start_date':start_date});
                this.setState({'end_date':end_date});


                start_date = start_date.replace(/-/g,'');
                end_date = end_date.replace(/-/g,'');

                var sql = "SELECT sum(count) as value,YYYYMMDD as name\
                            FROM qeeniao.api_call_summary\
                            where YYYYMMDD > "+start_date+" and YYYYMMDD <= "+end_date+"\
                            group by YYYYMMDD\
                            order by YYYYMMDD ASC";
                $.ajax({
                    url:this.props.url,
                    data:{sql:sql,'access_token':token},
                    type:'post',
                    dataType:'json',
                    success:function(o){
                        var tmp = [];
                        tmp.push(o);
                        if(this.isMounted()) {
                            this.setState({'data': tmp});
                        }
                    }.bind(this),
                    error:function(xhr,status,err){
                        console.error('获取数据失败!',status,err.toString());
                    }.bind(this)
                })
            }
        },
        handleSubmit:function(e){
            var start_date = React.findDOMNode(this.refs.from_date).value.trim();
            var end_date = React.findDOMNode(this.refs.to_date).value.trim();
			this.setState({'start':true});
            this.loadData(start_date,end_date);
        },
        componentDidMount:function(){
            $('#from_date').datetimepicker({
                language:'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2
            }).on('changeDate',function(ev){
                $('#to_date').datetimepicker('setStartDate',ev.date);
            }).on('outOfRange',function(){

            });

            $('#to_date').datetimepicker({
                language:'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2
            }).on('changeDate',function(ev){
                $('#from_date').datetimepicker('setEndDate',ev.date);
            }).on('outOfRange',function(){

            });
        },
        getInitialState:function(){
            return {
                'start_date':'',
                'end_date':'',
                'data':[],
				'start':false
            };
        },
        render:function(){
            var diagram;
			if(this.state.data.length > 0){
	            diagram = this.state.data.map(function(item){
	                return (
	                    <ApiAreaDiagram className="" id="MoreApiAreaDiagram" id_name="MoreApiAreaDiagram" y="调用量" title="API总调用量" data={item} />
	                );
	            });
			}else{
				if(this.state.start){
					diagram = <ReactLoading type='spinning-bubbles' color='#e3e3e3'/>
				}
			}
            

            return (
                <div className="modal fade" id={this.props.id} tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div className="modal-dialog">
                        <div className="modal-content">
                            <div className="modal-header">
                                <button type="button" className="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 className="modal-title" id="myModalLabel">更多图表</h4>
                            </div>

                            <div className="modal-body">
                                <div className="container">
                                    <form className="form-inline" id="dateForm" onSubmit={this.handleSubmit}>
                                        <div className="row">
                                            <div className="form-group">
                                                <label htmlFor="from_date">选择日期：</label>
                                                <input onChange={this.handleStartDate} className="form-control" size="16" type="text" value={this.state.start_date} id="from_date" ref="from_date" readOnly="true" data-date="" data-date-format="yyyy-mm-dd" data-link-field="dtp_input2" data-link-format="yyy-mm-dd"/>
                                            </div>

                                            <div className="form-group">
                                                <label htmlFor="to_date"> - </label>
                                                <input onChange={this.handleEndDate} className="form-control" size="16" type="text" value={this.state.end_date} id="to_date" ref="to_date" readOnly="true" data-date="" data-date-format="yyyy-mm-dd" data-link-field="dtp_input2" data-link-format="yyy-mm-dd"/>
                                            </div>

                                            <button type="submit" style={{marginLeft:'10px'}} className="btn btn-default btn-primary">提交</button>
                                        </div>
                                    </form>
                                </div>
                            </div>

                            <div className="modal-body">
                                {diagram}
                            </div>

                            <div className="modal-footer">
                                <button type="button" className="btn btn-default" data-dismiss="modal">退出</button>
                            </div>
                        </div>
                    </div>
                </div>
            );
        }
    });

});